﻿#region MIT License
/*
 Copyright (c) 2009-2010 Joxon Productions <joxonproductions@gmail.com>

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace SharpKeeperPluginLib
{
    public static class KPluginHelper
    {
        public static string GetPath(Type pluginType)
        {
            return pluginType.Assembly.Location;
        }

        public static string GetPath(Object obj)
        {
            if (obj == null)
                return "";

            return GetPath(obj.GetType());
        }


        public static string GetIdentifier(Type pluginType)
        {
            MemberInfo info = pluginType;
            object[] attributes = info.GetCustomAttributes(true);
                
            for (int i = 0; i < attributes.Length; i++)
            {
                if (attributes[i] is KPluginAttribute)
                {
                    return ((KPluginAttribute)attributes[i]).Identifier;
                }
            }

            return "";
        }

        public static string GetIdentifier(Object obj)
        {
            if (obj == null)
                return "";

            return GetIdentifier(obj.GetType());
        }

        public static string GetDescription(Type pluginType)
        {
            MemberInfo info = pluginType;
            object[] attributes = info.GetCustomAttributes(true);

            for (int i = 0; i < attributes.Length; i++)
            {
                if (attributes[i] is KPluginAttribute)
                {
                    return ((KPluginAttribute)attributes[i]).Description;
                }
            }

            return "";
        }

        public static string GetDescription(Object obj)
        {
            if (obj == null)
                return "";

            return GetDescription(obj.GetType());
        }


        public static string AssemblyCompany
        {
            get
            {
                var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }

        public static string AssemblyProduct
        {
            get
            {
                var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);

                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }


        public static string LocalDataFolder
        {
            get
            {
                try
                {
                    // First get the 'local application data' folder
                    string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

                    // Check if our company directory exists, if not, create it
                    appData = Path.Combine(appData, AssemblyCompany);

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    // Check if our application directory exists, if not, create it
                    appData = Path.Combine(appData, AssemblyProduct);

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    // check if our data folder exists, if not, create it
                    appData = Path.Combine(appData, "Data");

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    return appData;
                }
                catch
                {
                    // As a fallback, get the global temp path
                    return Path.GetTempPath();
                }
            }
        }

        public static string CommonDataFolder
        {
            get
            {
                try
                {
                    // First get the 'local application data' folder
                    string appData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

                    // Check if our company directory exists, if not, create it
                    appData = Path.Combine(appData, AssemblyCompany);

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    // Check if our application directory exists, if not, create it
                    appData = Path.Combine(appData, AssemblyProduct);

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    // check if our data folder exists, if not, create it
                    appData = Path.Combine(appData, "Data");

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    return appData;
                }
                catch
                {
                    // As a fallback, get the global temp path
                    return Path.GetTempPath();
                }
            }
        }

        public static string MyDocumentsFolder()
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        }

        public static string GetMyDocumentsFolder(string path)
        {
            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), path);
        }
        public static string LocalTempFolder
        {
            get
            {
                try
                {
                    // First get the 'local application data' folder
                    string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

                    // Check if our company directory exists, if not, create it
                    appData = Path.Combine(appData, AssemblyCompany);

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }


                    // Check if our application directory exists, if not, create it
                    appData = Path.Combine(appData, AssemblyProduct);

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    // check if our temp folder exists, if not, create it
                    appData = Path.Combine(appData, "Temp");

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    return appData;
                }
                catch
                {
                    // As a fallback, get the global temp path
                    return Path.GetTempPath();
                }
            }

        }

        public static string GetLocalDataFolder(string relativePath)
        {
            var strPath = LocalDataFolder;

            strPath = Path.Combine(strPath, relativePath);

            return strPath;
        }

        public static string ExecutingPath
        {
            get
            {
                var strPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

                return strPath;
            }
        }

        public static string GetExecutingPath(string relativePath)
        {
            var strPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            strPath = Path.Combine(strPath, relativePath);

            return strPath;
        }
    }
}